/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package carpool.services;

import carpool.HibernateUtil;
import carpool.persistence.fctuc.Users;
import java.beans.*;
import java.io.Serializable;

import java.text.SimpleDateFormat;
import org.hibernate.Session;


/**
 *
 * @author sdinis
 *
 * Esta classe serve de interface para os utilizadores harcoded
 * da FCTUC para a app Carpool usar no método de registo
 */
public class FCTUCUserBean implements Serializable {

    public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";
    private String sampleProperty;
    private PropertyChangeSupport propertySupport;

    public FCTUCUserBean() {
        propertySupport = new PropertyChangeSupport(this);
    }

    //"Users" = FCTUC user... refactoring later ...
    public Users getUser(int id) {

        Users u;
            Session session = HibernateUtil.getFCTUCSessionFactory().getCurrentSession();//.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            u = (Users) (session.createQuery("from Users as user where user.id=?").setInteger(0, id).uniqueResult());
            session.getTransaction().commit();
            return u;
    }

    public String toXML(Users u){
        if(u == null) return "<xml><error>Numero de estudante inexistente</error></xml>";

        StringBuilder sb = new StringBuilder("<xml>");
        sb.append("<firstname>"+u.getFirstName()+"</firstname>");
        sb.append("<lastname>"+u.getLastName()+"</lastname>");
        sb.append("<address>"+u.getAddressStreet()+"</address>");
        sb.append("<gender>"+u.getGender()+"</gender>");
        sb.append("<city>"+u.getAddressCity()+"</city>");
        sb.append("<zip1>"+u.getAddressZipCodeArea()+"</zip1>");
        sb.append("<zip2>"+u.getAddressZipCodeStreet()+"</zip2>");
        sb.append("<email>"+u.getEmail()+"</email>");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String date = sdf.format(u.getDateBirth());
        sb.append("<birth>"+date+"</birth>");

//        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        StreamResult streamResult = new StreamResult(os);
//        SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
//// SAX2.0 ContentHandler.
//        TransformerHandler hd = tf.newTransformerHandler();
//        Transformer serializer = hd.getTransformer();
//        serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
//        serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd");
//        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
//        hd.setResult(streamResult);
//        hd.startDocument();
//        AttributesImpl atts = new AttributesImpl();
//        hd.startElement("", "", "FCTUCUser", atts);
////identificacao
//        atts.clear();
//        atts.addAttribute("", "", "id", "integer", this.user.getId().toString());
//        atts.addAttribute("", "", "first_name", "string", this.user.getFirstName());
//        atts.addAttribute("", "", "last_name", "string", this.user.getLastName());
//        atts.addAttribute("", "", "date_bith", "string", this.user.getDateBirth().toString());
//        atts.addAttribute("", "", "gender", "string", String.valueOf(this.user.getGender()));
////morada
//        atts.clear();
//        hd.startElement("", "", "address", atts);
//        atts.addAttribute("", "", "street", "string", user.getAddressStreet());
//        atts.addAttribute("", "", "city", "string", user.getAddressCity());
//        atts.addAttribute("", "", "zip_area", "integer", Integer.toString(this.user.getAddressZipCodeArea()));
//        atts.addAttribute("", "", "zip_street", "integer", Integer.toString(this.user.getAddressZipCodeStreet()));
//        hd.endElement("", "", "address");
////contactos
//        atts.clear();
//        hd.startElement("", "", "contact", atts);
//        atts.addAttribute("", "", "email", "string", user.getEmail());
//        atts.addAttribute("", "", "phone", "string", user.getPhone());
//        hd.endElement("", "", "contact");
//        hd.endElement("", "", "FCTUCUser");
//        hd.endDocument();
        //sb.append("<msg>success</msg>");
        sb.append("</xml>");
        //return "<xml><error>Test error</error></xml>";
        return sb.toString();
        //return new String(os.toByteArray(), Charset.defaultCharset()); //"UTF-8");
    }

    public String getSampleProperty() {
        return sampleProperty;
    }

    public void setSampleProperty(String value) {
        String oldValue = sampleProperty;
        sampleProperty = value;
        propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }

    public String sugestID(String search) {
        return "this is a test";
//            Users u;
//            Session session = HibernateUtil.getFCTUCSessionFactory().getCurrentSession();//.getSessionFactory().getCurrentSession();
//            session.beginTransaction();
//            u = ((Users) session.createQuery("from Users where CAST(id AS CHAR) LIKE '%?%'").setString(0, search).uniqueResult());
//            session.getTransaction().commit();
//            if (u != null)
//                return Integer.toString(u.getId());
//            else
//                return "";
    }
}
